﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;

namespace InfFront.InfoSources.Base.Design
{
    internal class IconStackEditor : CollectionEditor
    {
        private object editValue = null;

        public IconStackEditor(Type type)
            : base(type)
        {
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            // copypasted this method from the ListViewGroupCollectionEditor
            // not sure if it makes sence or not
            this.editValue = value;
            object obj2 = base.EditValue(context, provider, value);
            this.editValue = null;
            return obj2;
        }

        protected override object CreateInstance(Type itemType)
        {
            var result = (IconStackEntry) base.CreateInstance(itemType);
            return result;
        }

        protected override string GetDisplayText(object value)
        {
            if (value == null)
                return string.Empty;

            var entry = (IconStackEntry) value;
            if (entry.Image == null)
                return "<empty>";

            return string.Format("{0}x{1}: {2}", entry.Image.Width, entry.Image.Height, entry.Style);
        }

        protected override object[] GetItems(object editValue)
        {
            //var editList = (IList) editValue;
            //if (editList.Count != 0)
            //{
            //    var host = Context.Container as IDesignerHost;
            //    if (host != null && host.RootComponent != Context.Instance)
            //    {
            //        var freshComponent = host.CreateFreshComponent(Context.Instance.GetType());
            //        if (freshComponent != null)
            //        {
            //            var freshList = (IList) Context.PropertyDescriptor.GetValue(freshComponent);
            //            var rv = new object[editList.Count - freshList.Count];
            //            for (int i = freshList.Count; i < editList.Count; ++i)
            //                rv[i - freshList.Count] = editList[i];
            //            return rv;
            //        }
            //    }
            //}
            return base.GetItems(editValue);
        }

        protected override object SetItems(object editValue, object[] value)
        {
            //var editList = (IList) editValue;
            //if (editList.Count != 0)
            //{
            //    var host = Context.Container as IDesignerHost;
            //    if (host != null && host.RootComponent != Context.Instance)
            //    {
            //        var freshComponent = host.CreateFreshComponent(Context.Instance.GetType());
            //        if (freshComponent != null)
            //        {
            //            var freshList = (IList) Context.PropertyDescriptor.GetValue(freshComponent);
            //            while (freshList.Count < editList.Count)
            //                editList.RemoveAt(editList.Count - 1);
            //            for (int i = 0; i < value.Length; ++i)
            //                editList.Add(value[i]);
            //            return editValue;
            //        }
            //    }
            //}
            return base.SetItems(editValue, value);
        }
    }
}
